//
//  CollectionView.m
//  Civacrm
//
//  Created by 王桂敏 on 2017/2/9.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "CollectionView.h"
#import "CollectionViewCell.h"
#import "CollectionViewCell1.h"
#import "CollectionViewCell2.h"
#import "CollectionViewCell3.h"

static NSString *CollectionIdentifier = @"CollectionViewCell";

static NSString *Collection1Identifier = @"CollectionViewCell1";

static NSString *Collection2Identifier = @"CollectionViewCell2";

static NSString *Collection3Identifier = @"CollectionViewCell3";

@implementation CollectionView
- (id)initViewWithType:(CollectionViewType)type
                 frame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setViewWithType:type];
    }
    return self;
}


- (void)setViewWithType:(CollectionViewType)type{
    self.type = type;
    
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    
    self.layout.minimumInteritemSpacing = 0;
    self.layout.minimumLineSpacing = 0;
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.layout];
    self.collectionView.backgroundColor = WhiteColor;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self addSubview:self.collectionView];
    
    
    switch (type) {
        case CollectionViewType0:
        {
            self.layout.itemSize = CGSizeMake(w(90), h(112));
            [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:CollectionIdentifier];
            
        }
            break;
        case CollectionViewType1:
        {
            self.selectIndex = 0;
            self.layout.itemSize = CGSizeMake(kScreenWidth / 3, h(42));
            [self.collectionView registerClass:[CollectionViewCell1 class] forCellWithReuseIdentifier:Collection1Identifier];
            
        }
            break;
        case CollectionViewType2:
        {
            self.selectIndex = 0;
            self.layout.itemSize = CGSizeMake(kScreenWidth / 4, h(76));
            [self.collectionView registerClass:[CollectionViewCell2 class] forCellWithReuseIdentifier:Collection2Identifier];
            
        }
            break;
        case CollectionViewType3:
        {
            self.selectIndex = 0;
            self.layout.itemSize = CGSizeMake(kScreenWidth / 4, h(164));
            [self.collectionView registerClass:[CollectionViewCell3 class] forCellWithReuseIdentifier:Collection3Identifier];
            
        }
            break;
            
        default:
            break;
    }
}


- (void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource = dataSource;
    if (self.type == CollectionViewType3) {
        if (!dataSource.count) {
            self.height = 0;
        } else {
            self.height = h(164);
        }
    }
    [self.collectionView reloadData];
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
}
#pragma mark - UICollectionViewDataSource -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (self.type) {
        case CollectionViewType0:
        {
            CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionIdentifier forIndexPath:indexPath];
            [cell configCellWithModel:self.dataSource[indexPath.row]];
            return cell;
            
        }
            break;
        case CollectionViewType1:
        {
            
            CollectionViewCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Collection1Identifier forIndexPath:indexPath];
            [cell configCellWithModel:self.dataSource[indexPath.row]];
            if (indexPath.row == self.selectIndex) {
                cell.titleLabel.textColor = ColorWithHexString(@"43b9f0");
            } else {
                cell.titleLabel.textColor = ColorWithHexString(@"a0c0d2");
            }
            return cell;
            
        }
            break;
        case CollectionViewType2:
        {
            
            CollectionViewCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Collection2Identifier forIndexPath:indexPath];
            [cell configCellWithModel:self.dataSource[indexPath.row]];
            if (indexPath.row == self.selectIndex) {
                cell.titleLabel.textColor = ColorWithHexString(@"43b9f0");
            } else {
                cell.titleLabel.textColor = ColorWithHexString(@"a0c0d2");
            }
            return cell;
            
        }
            break;
        case CollectionViewType3:
        {
            
            CollectionViewCell3 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Collection3Identifier forIndexPath:indexPath];
            [cell configCellWithModel:self.dataSource[indexPath.row]];
        
            return cell;
            
        }
            break;
            
        default:
            break;
    }
    
}

#pragma mark 处理点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectIndex = indexPath.row;
    [self.collectionView reloadData];
    if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView: didSelect: type:)]) {
        [self.delegate collectionView:self didSelect:indexPath.row type:self.type];
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout Method
#pragma mark 设置item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (self.type) {
        case CollectionViewType0:
        {
            return CGSizeMake(w(90), h(112));
            
        }
            break;
        case CollectionViewType1:
        {
            
            return CGSizeMake(kScreenWidth / 3, h(42));
            
        }
            break;
        case CollectionViewType2:
        {
            
            return CGSizeMake(kScreenWidth / 4, h(76));
            
        }
            break;
        case CollectionViewType3:
        {
            
            return CGSizeMake(kScreenWidth / 4, h(164));
            
        }
            break;
            
        default:
            break;
    }
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
